function Level(){
    Sprite.apply(this);
                
    this.skipButton;
    this.pauseButton;
    this.timer;
    this.bgColor = grey;
    this.render = render;
    
    
    var bg = new Bitmap(new BitmapData("pictures/levelbg.png"));
    bg.x = 0;
    bg.y = 0;
    this.addChild(bg);
    /*
    this.graphics.beginFill(this.bgColor);
    this.graphics.drawRect(0,0,cwidth, cheight);
    */
                
    function render(){
    //  this.fill(this.bgColor);
    //  this.graphics.drawRect(0,0,100, 100);
    }

    // Create Buttons
    skipButton = new Button("Skip");
    skipButton.x = cwidth - skipButton.width - MARGIN;  
    skipButton.y = cheight - skipButton.height - MARGIN;
    skipButton.addEventListener(MouseEvent.CLICK, clickSkip);
                
    pauseButton = new Button("Pause");
    pauseButton.x = cwidth - skipButton.width - pauseButton.width - 2*MARGIN;   
    pauseButton.y = cheight - pauseButton.height - MARGIN;
    pauseButton.addEventListener(MouseEvent.CLICK, clickPause);
                
    this.addChild(skipButton);
    this.addChild(pauseButton);
                
    // Create Timer
                
    timer = new Timer();
    timer.x = MARGIN;
    timer.y = MARGIN;
    timer.redraw();
                
    this.addChild(timer);
                
    this.run = function(e){
        timer.increment();
        timer.redraw();
    }
    
    
    this.levelSolved = levelSolved;
    function levelSolved(){
        this.nextLvlMenu = new NextLevelMenu();
        try{
            this.addChild(this.nextLvlMenu);
        }catch(ex){
            if(stage == undefined){
                console.debug("dade chyba more");
            }else{
                stage.addChild(this.nextLvlMenu);
            }    
        }
        window.clearInterval(intervalID);
    }
}
Level.prototype = new Sprite();
  
// Game Manager  

  
function clickSkip(e) {
    //alert("Are you shure you want to skip?");
    nextLevel();
    console.debug("level" + currentLevel);
}
function clickPause(e) {
    window.clearInterval(intervalID);
// PAUSE
}



var levelNames = ["levelCircuit","levelElements","levelFunctions"
,"levelLogic","levelOptic","levelPi","levelPythagoras"
,"levelSort", "levelTurtle"];
var jokeLevel = "levelJokes";
         
      
function mainStart(){
    showLevel(0);
}
            
function showLevel(i, w){
    var name = levelNames[i];
    var src = "levels/" + name + "/" + name + ".html";
    try{
        window.document.getElementById("level").setAttribute("src", src);
    }catch(e){
        window.parent.document.getElementById("level").setAttribute("src", src);
    }
}
            
function nextLevel(){
    var w = window;
    if(w.currentLevel < levelNames.length - 1){
        //w.currentLevel++;
        showLevel(w.currentLevel + 1);
    }else{
    // TODO posledny level  
    }
}
